package org.example.controller;

import org.example.model.BorrowRecord;
import org.example.service.BorrowRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/borrowRecord")
public class BorrowRecordController {
    @Autowired
    private BorrowRecordService borrowRecordService;

    //获取借阅记录列表（表格）
    //GET /api/borrowRecord/query
    //返回List<BorrowRecord>, 且BorrowRecord包含book/classes/student三个对象
    @RequestMapping("/query")
    public Object query(){
        List<BorrowRecord> records = borrowRecordService.query();
        return records;
    }

    //新增借阅记录
    //POST /api/borrowRecord/add
    //{"studentId":"8","bookId":"2","startTime":"2022-08-06 10:50:33","endTime":""}
    @RequestMapping("/add")
    public Object add(@RequestBody BorrowRecord borrowRecord){
        int n = borrowRecordService.add(borrowRecord);
        return null;
    }

    //查询借阅记录详情（修改时，需要先查询详情）
    //GET /api/borrowRecord/queryById?id=xxx
    @RequestMapping("/queryById")
    public Object queryById(Integer id){
        BorrowRecord borrowRecord = borrowRecordService.queryById(id);
        return borrowRecord;
    }

    //修改借阅记录
    //POST /api/borrowRecord/update
    //{"id":1,"studentId":"8","bookId":"2","startTime":"2022-08-06 10:50:33","endTime":""}
    @RequestMapping("/update")
    public Object update(@RequestBody BorrowRecord borrowRecord){
        int n = borrowRecordService.update(borrowRecord);
        return null;
    }

    //删除借阅记录（一个或多个）
    //GET /api/borrowRecord/delete?ids=xxx&ids=xxx
    @RequestMapping("/delete")
    public Object delete(@RequestParam List<Integer> ids){
        int n = borrowRecordService.delete(ids);
        return null;
    }
}
